package com.stress.testing.plugin.es.strategy;

import com.stress.testing.common.conf.AgentConfig;
import com.stress.testing.common.conf.PluginConfig;

public interface RequestIndexRenameStrategy {
    NoopRequestIndexRenameStrategy NOOP_REQUEST_INDEX_RENAME_STRATEGY = new NoopRequestIndexRenameStrategy();

    void renameIndex(Object arg);

    class NoopRequestIndexRenameStrategy implements RequestIndexRenameStrategy {

        @Override
        public void renameIndex(Object arg) {

        }
    }

    static RequestIndexRenameStrategy getForClass(Class<?> request, AgentConfig agentConfig,
                                                  PluginConfig pluginConfig) {
        String requestName = request.getSimpleName();
        String className = RequestIndexRenameStrategy.class.getPackage().getName() + "." + requestName +
                "IndexRenameStrategy";
        try {
            Class<?> clazz = RequestIndexRenameStrategy.class.getClassLoader().loadClass(className);
            Object instance = clazz.newInstance();
            AbstractRequestIndexRenameStrategy strategy = AbstractRequestIndexRenameStrategy.class.cast(instance);
            strategy.init(agentConfig, pluginConfig);
            return strategy;
        } catch (Exception e) {
            return NOOP_REQUEST_INDEX_RENAME_STRATEGY;
        }
    }
}
